package com.dxq.test.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ObjectUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dxq.test.R;
import com.dxq.test.adapter.GroupAdapter;
import com.dxq.test.db.Group;
import com.dxq.test.utils.DialogListClickListener;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * Author: zsh
 * Date: 2023/12/5
 * Describe: 自定义列表对话框
 */
public class GroupListDialog extends Dialog {

    private Context mContext;

    /**
     * 显示的标题
     */
    private TextView tvTitle;

    /**
     * 显示的消息
     */
    private TextView tvMessage;

    private RecyclerView recyclerView;

    /**
     * 确认和取消按钮
     */
    private TextView tvNegative, tvPositive;

    /**
     * 都是内容数据
     */
    private String message;
    private String title;
    private List<Group> recordList;
    private String positive, negative;
    private int topRes;
    private boolean isSingle;
    private GroupAdapter mAdapter;

    public DialogListClickListener dialogListClickListener;

    public void setDialogListClickListener(DialogListClickListener dialogListClickListener) {
        this.dialogListClickListener = dialogListClickListener;
    }

    public GroupListDialog(@NonNull Context context) {
        super(context);
        mContext = context;
    }

    public GroupListDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        mContext = context;
    }

    protected GroupListDialog(@NonNull Context context, boolean cancelable,
                              @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_dialog_group);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(true);
        //设置点击返回键不消失
        setCancelable(true);
        //初始化界面控件
        initView();
        //初始化界面数据
        refreshView();
        //初始化界面控件的事件
        initEvent();
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        tvTitle = findViewById(R.id.tvTitle);
        recyclerView = findViewById(R.id.recyclerView);

        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 1));
        recyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));//添加分割线
        mAdapter = new GroupAdapter(R.layout.item_group, recordList);//
        recyclerView.setAdapter(mAdapter);

        //item点击事件
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter,
                                    @NonNull @NotNull View view, int position) {
                //ToastUtils.showShort("item事件");
                if (dialogListClickListener!= null) {
                    dialogListClickListener.onClick(position);
                }
            }
        });
    }


    /**
     * 初始化界面控件的显示数据
     */
    private void refreshView() {
        //标题
        if (!TextUtils.isEmpty(title)) {
            tvTitle.setText(title);
        }
        if (!ObjectUtils.isEmpty(recordList)) {
            mAdapter.setList(recordList);
        }
    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听

    }

    public GroupListDialog setList(List<Group> list) {
        this.recordList = list;
        return this;
    }

    @Override
    public void show() {
        super.show();
        refreshView();
    }
}
